import {contextBridge, ipcRenderer, IpcRendererEvent, shell} from "electron"
import pkg from "../../package.json";

contextBridge.exposeInMainWorld("ipcRenderer", {
	send: (channel: string, args?: any) => ipcRenderer.send(channel, args),
	sendSync: (channel: string, args?: any) => ipcRenderer.sendSync(channel, args),
	on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) =>
		ipcRenderer.on(channel, listener),
	once: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) =>
		ipcRenderer.once(channel, listener),
	invoke: (channel: string, args: any) => ipcRenderer.invoke(channel, args),
	removeAllListeners: (channel: string) => ipcRenderer.removeAllListeners(channel),
	removeListener: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void) =>
		ipcRenderer.removeListener(channel, listener)
});

function platform() {
	if (process.platform === "darwin") return "Mac OS";
	return process.platform;
}

const release = process.getSystemVersion();
const arch = process.arch;

contextBridge.exposeInMainWorld("systemInfo", {
	platform: platform(),
	release: release,
	arch: arch,
	vue: () => pkg.dependencies.vue,
	node: () => process.versions.node,
	chrome: () => process.versions.chrome,
	electron: () => process.versions.electron
})

contextBridge.exposeInMainWorld("pkgInfo", pkg)

contextBridge.exposeInMainWorld("shell", shell)

contextBridge.exposeInMainWorld("crash", {
	start: () => {
		process.crash()
	}
})
